package com.example.demo2;

import com.example.tool.MysqlTool;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(name = "Forgetpassword", value = "/Forgetpassword")
public class Forgetpassword extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    private boolean isConSpeCharacters(String string) {
        // TODO Auto-generated method stub
        //如果不包含特殊字符
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(string);
        if (m.find()) {
            return true;
        }
        return false;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        MysqlTool tool =MysqlTool.getInstance();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String qq = request.getParameter("qq");
        String phonenumber = request.getParameter("phone");
        PrintWriter out = response.getWriter();
        if (username == null || username.length() == 0) {
            out.print("<script>alert('\\u8bf7\\u8f93\\u5165\\u7528\\u6237\\u540d!');window.history.back(-1)</script>");//请输入用户名
        } else if (qq == null || qq.length() == 0) {
            out.print("<script>alert('\\u8bf7\\u8f93\\u5165QQ\\u53f7\\u7801!');window.history.back(-1)</script>");//请输入qq号码
        } else if (phonenumber == null || phonenumber.length() == 0) {
            out.print("<script>alert('\\u8bf7\\u8f93\\u5165\\u624b\\u673a\\u53f7\\u7801!');window.history.back(-1)</script>");//请输入手机号码
        } else if (password == null || password.length() == 0) {
            out.print("<script>alert('\\u8bf7\\u8f93\\u5165\\u4fee\\u6539\\u7684\\u5bc6\\u7801!');window.history.back(-1)</script>");//请输入修改的密码
        } else if (password.length() < 8 || isConSpeCharacters(password)) {
            out.print("<script>alert('\\u4fee\\u6539\\u7684\\u5bc6\\u7801\\u9700\\u5927\\u4e8e8\\u4f4d\\u4e14\\u4e0d\\u80fd\\u542b\\u6709\\u4e2d\\u6587!');window.history.back(-1)</script>");//修改的密码需大于8位且不能含有中文
        } else {
            String sqlId = "select * from `user`.`user` where username=?";
            Object[] objId = {username};
            ResultSet rsId = tool.query(sqlId, objId);

            String sql = "select * from `user`.`user` where username=? and qq=? and phonenumber=?";

            Object[] objs = {username, qq, phonenumber};
            ResultSet rs = tool.query(sql, objs);

            try {
                if (!rsId.next()) {
                    out.print("<script>alert('\\u8d26\\u53f7\\u4e0d\\u5b58\\u5728!');window.history.back(-1)</script>");//账号不存在
                } else if (!rs.next()) {
                    out.print("<script>alert('\\u7528\\u6237\\u540d QQ \\u624b\\u673a\\u53f7\\u7801 \\u4e0d\\u5339\\u914d!');window.history.back(-1)</script>");//用户名 QQ 手机号码 不匹配
                } else {
                    String sqlup = "UPDATE `user`.`user` SET `password`=? where username=?";
                    Object[] objup = {password, username};
                    int a = tool.update(sqlup, objup);
                    if (a > 0) {
                        out.print("<script>alert('\\u4fee\\u6539\\u6210\\u529f\\uff0c\\u8bf7\\u7262\\u8bb0\\u65b0\\u5bc6\\u7801!');window.location.href='index.html'</script>");//修改成功，请牢记新密码
                    } else {
                        out.print("<script>alert('\\u4fee\\u6539\\u5931\\u8d25\\uff0c\\u8bf7\\u8054\\u7cfb\\u7ba1\\u7406\\u5458!');window.location.href='index.html'</script>");//修改失败，请联系管理员
                    }
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }// TODO 自动生成的 catch 块
        }
    }
}

